
package com.idealighter.game.games.sirenniuniu.struct;

import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.common.niuniu.NiuNiuCard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 四人牛牛桌子 .
 * 
 * @date 2016年1月20日 下午5:08:54
 *
 */

@Data
@EqualsAndHashCode(callSuper = true)
public class SiRenNiuNiuTable extends AbstractTable {

  // 座位数量
  private static final int SEAT_NUM = 4;

  private final SiRenNiuNiuRoom room;
  private final List<SiRenNiuNiuSeat> seats;
  // 桌子是否准备完成
  private boolean readied = false;

  // 一副牌
  private final List<NiuNiuCard> cards = Arrays.asList(NiuNiuCard.values());

  /**
   * 构造函数 .
   * 
   * @param id .
   * @param room .
   */
  public SiRenNiuNiuTable(int id, SiRenNiuNiuRoom room) {
    super(id);
    this.room = room;
    this.seats = createSeats();
  }

  /**
   * 创建位置 .
   * 
   * @return
   */
  private List<SiRenNiuNiuSeat> createSeats() {
    List<SiRenNiuNiuSeat> seats = new ArrayList<>(SEAT_NUM);
    for (int i = 0; i < SEAT_NUM; i++) {
      seats.add(new SiRenNiuNiuSeat(i, this));
    }

    return Collections.unmodifiableList(seats);
  }

  /**
   * 重置桌子 .
   */
  public void reset() {
    this.readied = false;
  }

  /**
   * 闲家合计筹码 .
   * 
   * @return
   */
  public long playesChips() {
    long playesChips = 0;
    for (SiRenNiuNiuSeat s : seats) {
      if (!s.isDealer() && s.getPlayerId() > 0) {
        playesChips += s.getTotalChips();
      }
    }

    return playesChips;
  }

  /**
   * 叫庄座位 .
   * 
   * @return
   */
  public List<SiRenNiuNiuSeat> callDealerSeats() {
    // 叫庄玩家
    List<SiRenNiuNiuSeat> callDealerSeats = new ArrayList<>();
    // 叫过庄的玩家
    List<SiRenNiuNiuSeat> calledDealerSeats = new ArrayList<>();
    for (SiRenNiuNiuSeat s : seats) {
      if (s.getCallDealer() != null) {
        calledDealerSeats.add(s);
        if (s.getCallDealer()) {
          callDealerSeats.add(s);
        }
      }
    }

    if (callDealerSeats.size() == 0) { // 没有人叫庄等于所有人叫庄
      callDealerSeats = calledDealerSeats;
    }

    return callDealerSeats;
  }

  /**
   * 牌桌是否全部摊牌 .
   * 
   * @return
   */
  public boolean showdown() {
    for (SiRenNiuNiuSeat s : seats) {
      if (s.getPlayerId() > 0 && !s.isShowdown()) {
        return false;
      }
    }

    return true;
  }

  /**
   * 叫庄是否结束,所有人都叫过庄且人数超过1个 .
   * 
   * @return
   */
  public boolean callDealerOver() {
    long callDealerNum = 0;
    for (SiRenNiuNiuSeat s : seats) {
      if (s.getPlayerId() > 0) {
        if (s.getCallDealer() == null) {
          return false;
        }
        callDealerNum++;
      }
    }

    if (callDealerNum <= 1) {
      return false;
    }

    return true;
  }

  /**
   * 牌桌是否下注结束 .
   * 
   * @return
   */
  public boolean betOver() {
    SiRenNiuNiuSeat dealer = dealer();
    for (SiRenNiuNiuSeat s : seats) {
      if (s.getPlayerId() > 0 && s.getPlayerId() != dealer.getPlayerId()
          && s.getBetedchips() == 0) {
        return false;
      }
    }

    return true;
  }

  /**
   * 返回庄家 .
   * 
   * @return
   */
  public SiRenNiuNiuSeat dealer() {
    for (SiRenNiuNiuSeat seat : seats) {
      if (seat.isDealer()) {
        return seat;
      }
    }

    return null;
  }

  /**
   * 获取seat .
   * 
   * @param order .
   * @return
   */
  public SiRenNiuNiuSeat getSeat(int order) {

    return seats.get(order);
  }

  @SuppressWarnings("unchecked")
  @Override
  public SiRenNiuNiuRoom room() {
    return room;
  }

  @Override
  public List<SiRenNiuNiuSeat> seats() {

    return seats;
  }

}
